package com.ari.actions;

import org.apache.log4j.Logger;

import com.devicemantra.coordinate.dao.Settings;
import com.devicemantra.coordinate.dao.SettingsDAO;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class EPPDUpdateSettingsAction {
	static final Logger logger = Logger
			.getLogger(EPPDUpdateSettingsAction.class);

	private String friendlyName;
	private String ipAddress;
	private String portNumber;
	private String username;
	private String password;
	private String extensions;

	@Expose
	@SerializedName("eppd_server")
	Settings eppdServerSettings = null;
	private String eppdServerName;
	private SettingsDAO settingsDAO;

	public void setEppdServerNameForUpdate(final String eppdServerName) {
		this.eppdServerName = eppdServerName;
	}

	/**
	 * @return
	 */
	public String execute() {
		try {
			Thread.sleep(1000);
		} catch (final InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/* Query the db now */
		try {
			eppdServerSettings = settingsDAO.getSetting(getFriendlyName());
		} catch (final Exception e) {
			System.out.println("Rolling back");
			e.printStackTrace();
		}

		if (eppdServerSettings != null) {
			eppdServerSettings.setFriendlyName(getFriendlyName());
			eppdServerSettings.setEppdUsername(getUsername());
			eppdServerSettings.setEppdPassword(getPassword());
			eppdServerSettings.setEppdIp(getIpAddress());
			eppdServerSettings.setEppdPort(getPortNumber());
			eppdServerSettings.setExtensions(getExtensions());
			settingsDAO.update(eppdServerSettings);
		}
		return "json";
	}

	public void validate() {
	}

	public void setSettingsDAO(final SettingsDAO settingsDAO) {
		this.settingsDAO = settingsDAO;
	}

	public String getFriendlyName() {
		return friendlyName;
	}

	public void setFriendlyName(final String friendlyName) {
		this.friendlyName = friendlyName;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(final String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getPortNumber() {
		return portNumber;
	}

	public void setPortNumber(final String portNumber) {
		this.portNumber = portNumber;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(final String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public String getExtensions() {
		return extensions;
	}

	public void setEditExtensions(final String extensions) {
		this.extensions = extensions;
	}

}
